package com.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dao.ExamDao;
import com.dao.ProanswerDao;
import com.dao.ProgrammingDao;
import com.model.ClazzProanswer;
import com.model.DifficultyInfo;
import com.model.Paper;
import com.model.Proanswer;
import com.model.Programming;
import com.model.RankInfo;
import com.model.Student;
import com.model.User;
import com.service.ExamService;
import com.util.Judge;
import com.util.getSystemTime;

@Component("ExamService")
public class ExamServiceImpl implements ExamService {

    public ProgrammingDao getProgrammingDao() {
        return programmingDao;
    }

    public void setProgrammingDao(ProgrammingDao programmingDao) {
        this.programmingDao = programmingDao;
    }

    public ExamDao getExamDao() {
        return examDao;
    }

    public void setExamDao(ExamDao examDao) {
        this.examDao = examDao;
    }

    @Resource
    private ExamDao examDao;
    @Resource(name = "ProgrammingDao")
    private ProgrammingDao programmingDao;
    @Resource(name = "ProanswerDao")
    private ProanswerDao proanswerDao;

    @Override
    public void saveAnswer(Programming pro, Paper paper, User user, String answer, int score, Timestamp submittime) {
        Proanswer proanswer = new Proanswer(pro, paper, user, answer, score, submittime);
        examDao.saveAnswer(proanswer);

    }

    @Override
    public void classSaveAnswer(Programming pro, Paper paper, Student stu, String answer, int score) {
        ClazzProanswer clazzProanswer = new ClazzProanswer(pro, paper, stu, answer, score);
        examDao.classSaveAnswer(clazzProanswer);
    }

    @Override
    public List checkUser(int id, String answer, int type, Paper paper, User user) {
        List list = null;
        Judge judge = new Judge();
        String stype; // 题目类型
        int time; // 运行时间
        int memory; // 运行内存
        String newAnswer = answer;
        Programming pro = programmingDao.findById(id);
        if (type == 0) {
            stype = "java";
            time = pro.getRuntimeforjava();
            memory = pro.getMemoryforjava();
        } else {
            stype = "c";
            time = pro.getRuntime();
            memory = pro.getMemoryforc();
        }
        String prefix = pro.getPrefix();
        if (prefix != null) {
        	newAnswer = prefix + answer  + pro.getPostfix();
        }

        String sid = id + "";
        String stime = time + "";
        String smemory = memory + "";
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++fefore judge"+"\n"+newAnswer);
        list = judge.inJudge(sid, stype, newAnswer, stime, smemory, String.valueOf(user.getId()));
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++out judge");
        System.out.println("程序执行结果是 ===" + list.get(1));
        int score = 0;
        if (list.get(1).equals("Answer Correct.")) {
            score = pro.getScore();
        }
        getSystemTime getIime = new getSystemTime();
        System.out.println("score is " + score);
        if(list.get(1).equals("Answer Correct.")){
        	saveAnswer(pro, paper, user, answer, score, new Timestamp(System.currentTimeMillis())); // 保存答案
        }else{
        	saveAnswer(pro, paper, user, answer, score, null); // 保存答案
        }
        list.add(2, score);
        list.add(3, getIime.getTime());
        return list;
    }

	@Override
    public List checkStudent(int id, String answer, int type, Paper paper, Student stu) {
        List list = null;
        Judge judge = new Judge();
        String stype; // 题目类型
        int time; // 运行时间
        int memory; // 运行内存
        String newAnswer = answer;
        Programming pro = programmingDao.findById(id);
        if (type == 0) {
            stype = "java";
            time = pro.getRuntimeforjava();
            memory = pro.getMemoryforjava();
        } else {
            stype = "c";
            time = pro.getRuntime();
            memory = pro.getMemoryforc();
        }
        String prefix = pro.getPrefix();
        if (prefix != null) {
        	newAnswer = prefix + answer  + pro.getPostfix();
        }
        String sid = id + "";
        String stime = time + "";
        String smemory = memory + "";
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++fefore judge");
        list = judge.inJudge(sid, stype, newAnswer, stime, smemory, stu.getUsername());
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++out judge");
        System.out.println("list 1111111111111111===" + list.get(1));
        int score = 0;
        if (list.get(1).equals("Answer Correct.")) {
            score = pro.getScore();
        }
        System.out.println("score is " + score);
        classSaveAnswer(pro, paper, stu, answer, score);
        list.add(2, score);
        getSystemTime getIime = new getSystemTime();
        list.add(3, getIime.getTime());
        return list;
    }

    @Override
    public List paperInfo(int uid, int pid, String flag) {

        return proanswerDao.paerInfo(uid, pid, flag);
    }

    @Override
    public List<Programming> getProgramList(int paperId, Integer uid, String flag) {
        return examDao.getProgramList(paperId, uid, flag);
    }

    @Override
    public List<?> showRank() {
        return proanswerDao.showRank();
    }

    @Override
    public List<RankInfo> rankInfo(int id) {
        List<RankInfo> information = new ArrayList<RankInfo>();
        List l = proanswerDao.rankInfo(id);
        for (Iterator iter = l.iterator(); iter.hasNext();) {
            Object[] obj = (Object[]) iter.next();
            information.add(new RankInfo((User) obj[0], (Long) obj[1], (Timestamp) obj[2]));
        }
        // System.out.println(l.size() + " "+information.size());
        return information;
    }

    @Override
    public List<?> showDifficulty() {
        return proanswerDao.showDifficulty();
    }

    @Override
    public List<DifficultyInfo> DifficultyInfo(int id) {
        List<DifficultyInfo> information = new ArrayList<DifficultyInfo>();
        List l = proanswerDao.difficultyInfo(id);
        for (Iterator iter = l.iterator(); iter.hasNext();) {
            Object[] obj = (Object[]) iter.next();

            information.add(new DifficultyInfo(programmingDao.findById(Integer.parseInt(obj[0] + "")), Integer.parseInt(obj[1] + ""), Integer.parseInt(obj[2] + "")));
        }
        return information;
    }

}
